package com.hw.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(name = "TServlet")
public class TServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Enumeration<String> hnames=request.getHeaderNames();
        String key=hnames.nextElement();
        System.out.println("=================================================");
        System.out.println("得到客户机请求方式: " + request.getMethod());
        System.out.println("--------------------------------------------");
        System.out.println("浏览器发出请求时的完整URL，包括协议 主机名 端口(如果有): " + request.getRequestURL());
        System.out.println("浏览器发出请求的资源名部分，去掉了协议和主机名: " + request.getRequestURI());
        System.out.println("返回请求资源所属于的WEB应用程序的路径:"+request.getContextPath());
        System.out.println("返回请求URL中的额外路径信息。额外路径信息是请求URL中的位于Servlet的路径之后和查询参数之前的内容，它以“/”开头:"+request.getPathInfo());
        System.out.println("返回URL中的额外路径信息所对应的资源的真实路径:"+request.getPathTranslated());
        System.out.println("Servlet的名称或Servlet所映射的路径:"+request.getServletPath());
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("返回请求行中的协议名和版本:"+request.getProtocol());
        System.out.println("请求行中的参数部分: " + request.getQueryString());
        System.out.println("浏览器所处于的客户机的IP地址: " + request.getRemoteAddr());
        System.out.println("浏览器所处于的客户机的主机名: " + request.getRemoteHost());
        System.out.println("浏览器所处于的客户机使用的网络端口: " + request.getRemotePort());
        System.out.println("服务器的IP地址: " + request.getLocalAddr());
        System.out.println("服务器的主机名: " + request.getLocalName());
        System.out.println("获取报头：" + request.getHeader(key));
        System.out.println("获取报头值的集合："+request.getHeaders(key));
        System.out.println(request.getContentType());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }
}
